package com.wust.smartcity.common;

import com.wust.smartcity.common.enums.ApplicationEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger.web.DocExpansion;
import springfox.documentation.swagger.web.OperationsSorter;
import springfox.documentation.swagger.web.UiConfiguration;
import springfox.documentation.swagger.web.UiConfigurationBuilder;
import java.util.ArrayList;
import java.util.List;

public abstract class SwaggerConfigBase {
    public Docket createRestApi() {
        List<Parameter> pars = new ArrayList<Parameter>();

        ParameterBuilder parameterBuilder1 = new ParameterBuilder();
        parameterBuilder1
                .name(ApplicationEnum.X_AUTH_TOKEN.getStringValue())
                .description("非开放接口专用参数（调用App接口和Web必填参数）：登陆成功后，服务端会返回一个令牌，请使用该令牌粘贴到此处。")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false).build();
        pars.add(parameterBuilder1.build());

        ParameterBuilder parameterBuilder2 = new ParameterBuilder();
        parameterBuilder2
                .name("appId")
                .description("开放接口专用参数（调用开放接口必填参数）：令牌标识，需要向服务接口提供方索取。")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false).build();
        pars.add(parameterBuilder2.build());

        ParameterBuilder parameterBuilder3 = new ParameterBuilder();
        parameterBuilder3
                .name("sign")
                .description("开放接口专用参数（调用开放接口必填参数）：签名，根据相关规则生成，具体规则咨询接口提供方。")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false).build();
        pars.add(parameterBuilder3.build());

        ParameterBuilder parameterBuilder4 = new ParameterBuilder();
        parameterBuilder4
                .name("nonce")
                .description("开放接口专用参数（调用开放接口必填参数）：请求唯一标识符。")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false).build();
        pars.add(parameterBuilder4.build());

        ParameterBuilder parameterBuilder5 = new ParameterBuilder();
        parameterBuilder5
                .name("timestamp")
                .description("开放接口专用参数（调用开放接口必填参数）：时间戳，如1587518030174")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false).build();
        pars.add(parameterBuilder5.build());
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(defaultTitleInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(getScanPackage()))
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build().globalOperationParameters(pars);
    }

    protected abstract String getScanPackage();

    protected abstract ApiInfo defaultTitleInfo();

    @Bean
    UiConfiguration uiConfig() {
        return UiConfigurationBuilder.builder().docExpansion(DocExpansion.LIST).operationsSorter(OperationsSorter.ALPHA).build();
    }
}
